Add site-wide options to disable all e-mail functions or only user-to-user email.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once('UserMailer.php');
12
13 /**
14 * consutrctor
15 */
16 function wfSpecialUserlogin() {
17 global $wgCommandLineMode;
18 global $wgRequest;
19 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
20 User::SetupSession();
21 }
22
23 $form = new LoginForm( $wgRequest );
24 $form->execute();
25 }
26
27 /**
28 *
29 * @package MediaWiki
30 * @subpackage SpecialPage
31 */
32 class LoginForm {
33 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
34 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
35 var $mLoginattempt, $mRemember, $mEmail;
36
37 function LoginForm( &$request ) {
38 global $wgLang, $wgAllowRealName, $wgEnableEmail;
39
40 $this->mName = $request->getText( 'wpName' );
41 $this->mPassword = $request->getText( 'wpPassword' );
42 $this->mRetype = $request->getText( 'wpRetype' );
43 $this->mReturnto = $request->getVal( 'returnto' );
44 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
45 $this->mPosted = $request->wasPosted();
46 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
47 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
48 && $wgEnableEmail;
49 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
50 && $wgEnableEmail;
51 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
52 $this->mAction = $request->getVal( 'action' );
53 $this->mRemember = $request->getCheck( 'wpRemember' );
54
55 if( $wgEnableEmail ) {
56 $this->mEmail = $request->getText( 'wpEmail' );
57 } else {
58 $this->mEmail = '';
59 }
60 if( $wgAllowRealName ) {
61 $this->mRealName = $request->getText( 'wpRealName' );
62 } else {
63 $this->mRealName = '';
64 }
65
66 # When switching accounts, it sucks to get automatically logged out
67 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
68 $this->mReturnto = '';
69 }
70 }
71
72 function execute() {
73 if ( !is_null( $this->mCookieCheck ) ) {
74 $this->onCookieRedirectCheck( $this->mCookieCheck );
75 return;
76 } else if( $this->mPosted ) {
77 if( $this->mCreateaccount ) {
78 return $this->addNewAccount();
79 } else if ( $this->mCreateaccountMail ) {
80 return $this->addNewAccountMailPassword();
81 } else if ( $this->mMailmypassword ) {
82 return $this->mailPassword();
83 } else if ( ( 'submit' == $this->mAction ) || $this->mLoginattempt ) {
84 return $this->processLogin();
85 }
86 }
87 $this->mainLoginForm( '' );
88 }
89
90 /**
91 * @access private
92 */
93 function addNewAccountMailPassword() {
94 global $wgOut;
95
96 if ('' == $this->mEmail) {
97 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
98 return;
99 }
100
101 $u = $this->addNewaccountInternal();
102
103 if ($u == NULL) {
104 return;
105 }
106
107 $u->saveSettings();
108 $error = $this->mailPasswordInternal($u);
109
110 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
111 $wgOut->setRobotpolicy( 'noindex,nofollow' );
112 $wgOut->setArticleRelated( false );
113
114 if ( $error === '' ) {
115 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
116 $wgOut->returnToMain( false );
117 } else {
118 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
119 }
120
121 $u = 0;
122 }
123
124
125 /**
126 * @access private
127 */
128 function addNewAccount() {
129 global $wgUser, $wgOut;
130 global $wgDeferredUpdateList;
131
132 $u = $this->addNewAccountInternal();
133
134 if ($u == NULL) {
135 return;
136 }
137
138 $wgUser = $u;
139 $wgUser->setCookies();
140
141 $up = new UserUpdate();
142 array_push( $wgDeferredUpdateList, $up );
143
144 if( $this->hasSessionCookie() ) {
145 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
146 } else {
147 return $this->cookieRedirectCheck( 'new' );
148 }
149 }
150
151
152 /**
153 * @access private
154 */
155 function addNewAccountInternal() {
156 global $wgUser, $wgOut;
157 global $wgMaxNameChars;
158 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
159
160 if (!$wgUser->isAllowedToCreateAccount()) {
161 $this->userNotPrivilegedMessage();
162 return;
163 }
164
165 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
166 $this->mainLoginForm( wfMsg( 'badretype' ) );
167 return;
168 }
169
170 $name = trim( $this->mName );
171 $u = User::newFromName( $name );
172 if ( is_null( $u ) ||
173 ( '' == $name ) ||
174 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
175 (strpos( $name, "/" ) !== false) ||
176 (strlen( $name ) > $wgMaxNameChars) ||
177 ucFirst($name) != $u->getName() )
178 {
179 $this->mainLoginForm( wfMsg( 'noname' ) );
180 return;
181 }
182 if ( wfReadOnly() ) {
183 $wgOut->readOnlyPage();
184 return;
185 }
186
187 if ( 0 != $u->idForName() ) {
188 $this->mainLoginForm( wfMsg( 'userexists' ) );
189 return;
190 }
191
192 if ( $wgAccountCreationThrottle ) {
193 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
194 $value = $wgMemc->incr( $key );
195 if ( !$value ) {
196 $wgMemc->set( $key, 1, 86400 );
197 }
198 if ( $value > $wgAccountCreationThrottle ) {
199 $this->throttleHit( $wgAccountCreationThrottle );
200 return;
201 }
202 }
203
204 return $this->initUser( $u );
205 }
206
207 /**
208 * Actually add a user to the database.
209 * Give it a User object that has been initialised with a name.
210 *
211 * @param User $u
212 * @return User
213 * @access private
214 */
215 function &initUser( &$u ) {
216 $u->addToDatabase();
217 $u->setPassword( $this->mPassword );
218 $u->setEmail( $this->mEmail );
219 $u->setRealName( $this->mRealName );
220
221 global $wgAuth;
222 $wgAuth->initUser( $u );
223
224 if ( $this->mRemember ) { $r = 1; }
225 else { $r = 0; }
226 $u->setOption( 'rememberpassword', $r );
227
228 return $u;
229 }
230
231 /**
232 * @access private
233 */
234 function processLogin() {
235 global $wgUser;
236 global $wgDeferredUpdateList;
237
238 if ( '' == $this->mName ) {
239 $this->mainLoginForm( wfMsg( 'noname' ) );
240 return;
241 }
242 $u = User::newFromName( $this->mName );
243 if( is_null( $u ) ) {
244 $this->mainLoginForm( wfMsg( 'noname' ) );
245 return;
246 }
247 $id = $u->idForName();
248 if ( 0 == $id ) {
249 global $wgAuth;
250 /**
251 * If the external authentication plugin allows it,
252 * automatically create a new account for users that
253 * are externally defined but have not yet logged in.
254 */
255 if( $wgAuth->autoCreate() &&
256 $wgAuth->userExists( $u->getName() ) &&
257 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
258 $u =& $this->initUser( $u );
259 } else {
260 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
261 return;
262 }
263 } else {
264 $u->setId( $id );
265 $u->loadFromDatabase();
266 }
267 if (!$u->checkPassword( $this->mPassword )) {
268 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
269 return;
270 }
271
272 # We've verified now, update the real record
273 #
274 if ( $this->mRemember ) {
275 $r = 1;
276 } else {
277 $r = 0;
278 }
279 $u->setOption( 'rememberpassword', $r );
280
281 $wgUser = $u;
282 $wgUser->setCookies();
283
284 $up = new UserUpdate();
285 array_push( $wgDeferredUpdateList, $up );
286
287 if( $this->hasSessionCookie() ) {
288 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
289 } else {
290 return $this->cookieRedirectCheck( 'login' );
291 }
292 }
293
294 /**
295 * @access private
296 */
297 function mailPassword() {
298 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
299 global $wgCookiePath, $wgCookieDomain, $wgDBname;
300
301 if ( '' == $this->mName ) {
302 $this->mainLoginForm( wfMsg( 'noname' ) );
303 return;
304 }
305 $u = User::newFromName( $this->mName );
306 if( is_null( $u ) ) {
307 $this->mainLoginForm( wfMsg( 'noname' ) );
308 return;
309 }
310 $id = $u->idForName();
311 if ( 0 == $id ) {
312 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
313 return;
314 }
315 $u->setId( $id );
316 $u->loadFromDatabase();
317
318 $error = $this->mailPasswordInternal( $u );
319 if ($error === '') {
320 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
321 } else {
322 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
323 }
324
325 }
326
327
328 /**
329 * @access private
330 */
331 function mailPasswordInternal( $u ) {
332 global $wgDeferredUpdateList, $wgOutputEncoding;
333 global $wgPasswordSender, $wgDBname, $wgIP;
334 global $wgCookiePath, $wgCookieDomain;
335
336 if ( '' == $u->getEmail() ) {
337 return wfMsg( 'noemail', $u->getName() );
338 }
339 $np = User::randomPassword();
340 $u->setNewpassword( $np );
341
342 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
343 $u->saveSettings();
344
345 $ip = $wgIP;
346 if ( '' == $ip ) { $ip = '(Unknown)'; }
347
348 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
349
350 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
351
352 return htmlspecialchars( $error );
353 }
354
355
356 /**
357 * @access private
358 */
359 function successfulLogin( $msg ) {
360 global $wgUser;
361 global $wgDeferredUpdateList;
362 global $wgOut;
363
364 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
365 $wgOut->setRobotpolicy( 'noindex,nofollow' );
366 $wgOut->setArticleRelated( false );
367 $wgOut->addWikiText( $msg );
368 $wgOut->returnToMain();
369 }
370
371 function userNotPrivilegedMessage() {
372 global $wgOut, $wgUser, $wgLang;
373
374 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
375 $wgOut->setRobotpolicy( 'noindex,nofollow' );
376 $wgOut->setArticleRelated( false );
377
378 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
379
380 $wgOut->returnToMain( false );
381 }
382
383 /**
384 * @access private
385 */
386 function mainLoginForm( $err ) {
387 global $wgUser, $wgOut, $wgLang;
388 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
389
390 if ( '' == $this->mName ) {
391 if ( 0 != $wgUser->getID() ) {
392 $this->mName = $wgUser->getName();
393 } else {
394 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
395 }
396 }
397
398 $q = 'action=submit';
399 if ( !empty( $this->mReturnto ) ) {
400 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
401 }
402 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
403
404
405 require_once( 'templates/Userlogin.php' );
406 $template =& new UserloginTemplate();
407
408 $template->set( 'name', $this->mName );
409 $template->set( 'password', $this->mPassword );
410 $template->set( 'retype', $this->mRetype );
411 $template->set( 'email', $this->mEmail );
412 $template->set( 'realname', $this->mRealName );
413
414 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
415 $template->set( 'error', $err );
416 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
417 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
418 $template->set( 'userealname', $wgAllowRealName );
419 $template->set( 'useemail', $wgEnableEmail );
420 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
421
422 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
423 $wgOut->setRobotpolicy( 'noindex,nofollow' );
424 $wgOut->setArticleRelated( false );
425 $wgOut->addTemplate( $template );
426 }
427
428 /**
429 * @access private
430 */
431 function hasSessionCookie() {
432 global $wgDisableCookieCheck;
433 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
434 }
435
436 /**
437 * @access private
438 */
439 function cookieRedirectCheck( $type ) {
440 global $wgOut, $wgLang;
441
442 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
443 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
444
445 return $wgOut->redirect( $check );
446 }
447
448 /**
449 * @access private
450 */
451 function onCookieRedirectCheck( $type ) {
452 global $wgUser;
453
454 if ( !$this->hasSessionCookie() ) {
455 if ( $type == 'new' ) {
456 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
457 } else if ( $type == 'login' ) {
458 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
459 } else {
460 # shouldn't happen
461 return $this->mainLoginForm( wfMsg( 'error' ) );
462 }
463 } else {
464 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
465 }
466 }
467
468 /**
469 * @access private
470 */
471 function throttleHit( $limit ) {
472 global $wgOut;
473
474 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
475 }
476 }
477 ?>